package com.tjetc.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @since 2025-11-04
 */
@Configuration
public class RabbitMQConfig {

    // 基础队列
    public static final String BASIC_QUEUE = "queue";

    // 基础队列
    @Bean
    public Queue queue() {
        return new Queue(BASIC_QUEUE);
    }

    // fanout模式
    public static final String FANOUT_EXCHANGE = "fanoutExchange";
    public static final String FANOUT_QUEUE1 = "fanoutQueue1";
    public static final String FANOUT_QUEUE2 = "fanoutQueue2";

    // fanout模式
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Queue fanoutQueue1() {
        return new Queue(FANOUT_QUEUE1);
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue(FANOUT_QUEUE2);
    }

    // 注意：将交换机和队列绑定在一起
    @Bean
    public Binding fanoutBinding1() {
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBinding2() {
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

    // direct模式
    public static final String DIRECT_EXCHANGE = "directExchange";
    public static final String DIRECT_QUEUE1 = "directQueue1";
    public static final String DIRECT_QUEUE2 = "directQueue2";
    public static final String ROUTING_KEY1 = "routingKey1";
    public static final String ROUTING_KEY2 = "routingKey2";


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue directQueue1() {
        return new Queue(DIRECT_QUEUE1);
    }

    @Bean
    public Queue directQueue2() {
        return new Queue(DIRECT_QUEUE2);
    }

    @Bean
    public Binding directBinding1() {
        return BindingBuilder.bind(directQueue1()).to(directExchange()).with(ROUTING_KEY1);
    }

    @Bean
    public Binding directBinding2() {
        return BindingBuilder.bind(directQueue2()).to(directExchange()).with(ROUTING_KEY2);
    }


    // Topic
    public static final String TOPIC_EXCHANGE = "topicExchange";
    public static final String TOPIC_QUEUE1 = "topicQueue1";
    public static final String TOPIC_QUEUE2 = "topicQueue2";
    //
    public static final String ROUTING_KEY3 = "*.orange.*";
    public static final String ROUTING_KEY4 = "lazy.#";


    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }


    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE1);
    }

    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE2);
    }

    @Bean
    public Binding topicBinding1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(ROUTING_KEY3);
    }

    @Bean
    public Binding topicBinding2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ROUTING_KEY4);
    }

}
